<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Pegawai extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'mst_pegawai';

		$this->primary_key = 'mst_agama.pegawai_id';

	}
	function get_pegawai($aktif=null){
		$this->load->database();
			$filter = "";
			if($aktif != null)
				$filter = "WHERE {$this->table_name}.aktif='$aktif'";
				
			$sql = "SELECT mst_pegawai.*,mst_agama.agama,mst_suku.suku,mst_bagian_pegawai.bagian FROM mst_pegawai JOIN mst_agama ON mst_pegawai.agama_id=mst_agama.agama_id JOIN mst_suku ON mst_pegawai.suku_id=mst_suku.suku_id JOIN mst_bagian_pegawai ON mst_bagian_pegawai.bagian_pegawai_id=mst_pegawai.bagian_pegawai_id $filter ORDER BY mst_pegawai.pegawai_id;";
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_pegawai_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM mst_pegawai WHERE pegawai_id='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert('mst_pegawai',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where('pegawai_id',$id);
				$this->db->update('mst_pegawai',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM mst_pegawai WHERE pegawai_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>